Ext.namespace('GBIF');
GBIF.SSGrid = function(config){
	
	this.pageSize = 100;
	
	var store = new Ext.data.JsonStore({
			url: '../../resources/api/savedSearch.php'
		,	baseParams: { 
					cmd: 'listSavedSearch'
				,	limit: this.pageSize
				,	assocFlag: 'null'
			}
		,	autoLoad: true	
		,	root: 'records'
		,	totalProperty: 'recordCount'
		,	fields: [	
					{name: 'autoId', mapping: 'autoId'}
				,	{name: 'query', mapping: 'query'}	
				,	{name: 'searchType', mapping: 'searchType'}
				,	{name: 'associationCount', mapping: 'associationCount'}
				,	{name: 'dateAdded', mapping: 'dateAdded' }
			]			
		,	remoteSort: true
		,	sortInfo: { field: "dateAdded", direction: "DESC" }
		,	listeners: {
				loadexception: function() {
					GBIF.Notice.msg('Error','Error in connection.');
				}
			}
	});
	
	var columns = [
			{header: 'Auto Id', width: 60, sortable: true, dataIndex: 'autoId'}
		,	{header: 'Query', width: 120,  sortable: true, dataIndex: 'query'}
		,	{header: 'Search Type', width: 100, sortable: true, dataIndex: 'searchType'}
		,	{header: '# of Assoc.', width: 60, sortable: true, dataIndex: 'associationCount'}
		,	{header: 'Date Searched', width: 120, sortable: true, dataIndex: 'dateAdded'}
	]
	
	var filters = new Ext.ux.grid.GridFilters({
			encode: true
		,	filters: [{
					type: 'string'
				,	dataIndex: 'autoId'
			}, {
					type: 'string'
				,	dataIndex: 'query'
			}, {
					type: 'string'
				,	dataIndex: 'searchType'
			},{
					type: 'date'
				,	dataIndex: 'dateAdded'
			}]
	});
	
	this.sm =	new Ext.grid.RowSelectionModel({
			singleSelect: true
		,	listeners: {
					rowselect: function(sm, row, rec){
						this.fireEvent('rowselected', rec, this);
					}
				,	rowdeselect: function(sm, row, rec){
						this.fireEvent('rowdeselected', rec, this);
					}
				,	scope: this	
			}
	});
	
	Ext.apply(this, config, {
			store: store
		,	columns: columns
		,	sm: this.sm
		,	plugins: [filters]
		,	autoScroll: true
		, 	loadMask: true
		,	border: false
		,	viewConfig: {
					forceFit: true
				,	deferEmptyText: false
				,	emptyText: 'No Resources Available.'
			}
		,	tbar: [{
						text: 'Add Association'
					,	scope: this	
					,	handler: function(){
							var selected = this.getSelectionModel().getSelected();
							if(this.getStore().getCount() != 0){
								if(Ext.isDefined(selected)){
									var addWindow = new GBIF.AddAssociation();
									addWindow.show();
									addWindow.on('afterAssocAdd', function(){
										this.fireEvent('afterAssocAdd', this);
									}, this);
								} else {
									GBIF.Notice.msg('Notice','Please select record.');
								}
							}else {
								GBIF.Notice.msg('Notice','No Recources available.');
							}
						}
				}, {
					xtype: 'cycle'
				,	showText: true
				,	width: 100
				,	scope: this
				,	items: [{
							text: 'All'
						,	checked:true
					},{
							text: 'Associations'
					},{
							text: 'No Associations'
					}]
				,	changeHandler: function(btn, item){
							var flag = null;
							switch(item.text){
								case 'Associations':
									flag = 'true';
									break;
								case 'No Associations':
									flag = 'false';
									break;
								case 'All':
									flag = 'null';
									break
							}
						this.store.baseParams['assocFlag'] = flag ;
						this.store.reload();
					}
			}]	
		,	bbar: new Ext.PagingToolbar({
					pageSize: this.pageSize
				,	scope: this	
				,	store: store
				,	displayInfo: true
				,	displayMsg: 'Displaying Resources {0} - {1} of {2}'
				,	emptyMsg: 'No Resources Available.'	
			})
		
	});

	GBIF.SSGrid.superclass.constructor.call(this, config);
};

Ext.extend(GBIF.SSGrid, Ext.grid.GridPanel, {});